package com.fancy.learncenter.activity;

import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.adapter.ReleaseWDPIcAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.common.StatusBarCompat;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.CustomProgressDialog;
import com.fancy.learncenter.view.PopupWindowCity;
import com.fancy.learncenter.view.PopupWindowCutPic;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowExperienceType;
import com.fancy.learncenter.view.PopupWindowRewardType;
import com.fancy.learncenter.view.PopupWindowSalaryType;
import com.fancy.learncenter.view.PopupWindowWorkType;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;
import com.superservice.lya.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReleaseZhiWeiActivity extends TakePhotoActivity {

    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.work_tv)
    TextView workTv;
    @Bind(R.id.salary_tv)
    TextView salaryTv;
    @Bind(R.id.time_tv)
    TextView timeTv;
    @Bind(R.id.education_tv)
    TextView educationTv;
    @Bind(R.id.work_type)
    RelativeLayout workType;
    @Bind(R.id.company_logo)
    SimpleDraweeView companyLogo;
    @Bind(R.id.company_num)
    TextView companyNum;
    @Bind(R.id.company_name)
    EditText companyName;
    @Bind(R.id.work_name)
    EditText workName;
    @Bind(R.id.place_detail_et)
    EditText placeDetailEt;
    @Bind(R.id.location_tv)
    TextView locationTv;
    @Bind(R.id.reward_tv)
    TextView rewardTv;
    @Bind(R.id.description_tv)
    EditText descriptionTv;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_zhiwei);
        ButterKnife.bind(this);
        StatusBarCompat.compat(this, getResources().getColor(R.color.toolbar_color));
        title.setText("发布职位");
        takePhoto = getTakePhoto();
        takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
        initRecycleView();
    }

    TakePhoto takePhoto;
    PopupWindowWorkType windowWorkType;
    PopupWindowEducationType educationType;
    PopupWindowCity popupWindowCity;
    PopupWindowSalaryType salaryType;
    PopupWindowExperienceType experienceType;
    PopupWindowRewardType rewardType;

    String educationId;

    @OnClick({R.id.work_type, R.id.back, R.id.location_type, R.id.galary_rl, R.id.submit, R.id.company_num_ll, R.id.logo_ll, R.id.salary_type, R.id.time_type, R.id.eduction_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.work_type:
                if (windowWorkType == null) {
                    windowWorkType = new PopupWindowWorkType(this, workType);
                    windowWorkType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            workTv.setText(content);
                        }


                    });
                }
                windowWorkType.showPopupWindow();
                break;
            case R.id.galary_rl:

                if (rewardType == null) {
                    rewardType = new PopupWindowRewardType(this, workType);
                    rewardType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            rewardTv.setText(content);

                            rewardId = id;
                        }


                    });
                }

                rewardType.showPopupWindow();
                break;
            case R.id.company_num_ll:
                if (windowWorkType == null) {
                    windowWorkType = new PopupWindowWorkType(this, workType);
                    windowWorkType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            workTv.setText(content);
                        }

                    });
                }
                windowWorkType.showPopupWindow();
                break;
            case R.id.location_type:

                if (popupWindowCity == null) {
                    popupWindowCity = new PopupWindowCity(this);
                    popupWindowCity.setSelectCallBack(new PopupWindowCity.SelectCallBack() {
                        @Override
                        public void selectCallBack(String province, String city, String area, String id) {
                            locationTv.setText(province + city + area);

                            locationId = id;
                        }
                    });
                }
                popupWindowCity.ShowPickerView();

                break;
            case R.id.salary_type:

                if (salaryType == null) {
                    salaryType = new PopupWindowSalaryType(this, workType);
                    salaryType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            salaryTv.setText(content);
                            salaryId = id;
                        }

                    });
                }
                salaryType.showPopupWindow();

                break;
            case R.id.time_type:

                if (experienceType == null) {
                    experienceType = new PopupWindowExperienceType(this, workType);

                    experienceType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            timeTv.setText(content);
                            yearId = id;
                        }

                    });


                }
                experienceType.showPopupWindow();

                break;
            case R.id.eduction_type:
                if (educationType == null) {
                    educationType = new PopupWindowEducationType(this, workType);
                    educationType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            educationTv.setText(content);

                            educationId = id;

                        }

                    });
                }
                educationType.showPopupWindow();
                break;
            case R.id.submit:
                getDatas();
                break;
            case R.id.logo_ll:

                if (popupWindowCutPic == null) {
                    popupWindowCutPic = new PopupWindowCutPic(workType);
                    popupWindowCutPic.setOnClickListener(new PopupWindowCutPic.CutPicCallBack() {
                        @Override
                        public void choose() {
//
                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                            takePhoto.onPickFromGalleryWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                        }

                        @Override
                        public void cancel() {

                        }

                        @Override
                        public void camera() {
                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                            takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                        }

                        @Override
                        public void see() {

                        }
                    });

                }
                isCompanyLogo = true;
                popupWindowCutPic.isShow();
                break;
        }
    }

    PopupWindowCutPic popupWindowCutPic;

    boolean isCompanyLogo = false;

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);

        if (isCompanyLogo) {
            if (result.getImage() != null && !TextUtils.isEmpty(result.getImage().getCompressPath())) {
                picPath = result.getImage().getCompressPath();
                companyLogo.setImageURI(Uri.fromFile(new File(result.getImage().getCompressPath())));
            }
        } else {

            ArrayList<TImage> images = result.getImages();

            for (int i = 0; i < images.size(); i++) {
//                picData.add(images.get(i).getCompressPath());

                releaseWDPIcAdapter.itemDatas.add(releaseWDPIcAdapter.itemDatas.size() - 1, images.get(i).getCompressPath());

            }
            releaseWDPIcAdapter.notifyDataSetChanged();

//            if (result.getImage() != null && !TextUtils.isEmpty(result.getImage().getCompressPath())) {
//                picData.add(result.getImage().getCompressPath());
//            }
//
//            ArrayList<String> itemDatas = new ArrayList();
//            itemDatas.addAll(picData);
//            itemDatas.add("res://com.superservice.lya/" + R.mipmap.ic_tianjia);
//            releaseWDPIcAdapter.notifyDataSetChanged(itemDatas);
        }


    }

    @Override
    public void finish() {
        super.finish();
    }

    String picPath = "";
    String locationId = "";
    String yearId = "";
    String salaryId = "";
    String rewardId = "";

    Dialog dialog;
    boolean isSubmit = false;

    private void disDia() {

        isSubmit = false;
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void getDatas() {

        if (TextUtils.isEmpty(companyName.getText().toString())) {
            ToastUtil.show("请输入公司名称");
            return;
        }


        if (TextUtils.isEmpty(picPath)) {
            ToastUtil.show("请选择公司LOGO");
            return;
        }


        if (TextUtils.isEmpty(workName.getText().toString())) {
            ToastUtil.show("请输入职位名称");
            return;
        }


        if (TextUtils.isEmpty(locationId)) {
            ToastUtil.show("请选择工作区域");
            return;
        }

        if (TextUtils.isEmpty(placeDetailEt.getText().toString())) {
            ToastUtil.show("请输入公司详细地址");
            return;
        }


        if (TextUtils.isEmpty(rewardId)) {
            ToastUtil.show("请选择公司福利");
            return;
        }


        if (TextUtils.isEmpty(salaryId)) {
            ToastUtil.show("请选择薪资范围");
            return;
        }

        if (TextUtils.isEmpty(yearId)) {
            ToastUtil.show("请选择经验要求");
            return;
        }

        if (TextUtils.isEmpty(educationId)) {
            ToastUtil.show("请选择学历要求");
            return;
        }


        if (TextUtils.isEmpty(descriptionTv.getText().toString())) {
            ToastUtil.show("请输入职位描述");
            return;
        }

        if (isSubmit) {
            return;
        } else {
            isSubmit = true;
            if (dialog == null) {
                dialog = CustomProgressDialog.creatRequestDialog(this);
            }
            dialog.show();

        }

        Map map = new HashMap();
        map.put("companyName", companyName.getText().toString());

        map.put("welfare", rewardId);

        map.put("companyLogo", "data:image/jpeg;base64," + Utils.imageToBase64(picPath));

        map.put("position", workName.getText().toString());

        map.put("salary", salaryId);


        map.put("education", educationId);

        map.put("workYear", yearId);

        map.put("locationId", locationId);

        map.put("place", placeDetailEt.getText().toString());

        map.put("content", descriptionTv.getText().toString());

        List<String> datas = new ArrayList();
        for (int i = 0; i < releaseWDPIcAdapter.itemDatas.size() - 1; i++) {
            datas.add("data:image/jpeg;base64," + Utils.imageToBase64(releaseWDPIcAdapter.itemDatas.get(i)));
        }

        String[] array = datas.toArray(new String[0]);
        HttpMehtod.getInstance().releaseWork(map, array, new IdeaObserver<BaseDataBean>(this) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                disDia();
                ToastUtil.show("发布成功");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

                disDia();
            }
        });
    }

    ArrayList<String> picData = new ArrayList<>();
    ReleaseWDPIcAdapter releaseWDPIcAdapter;

    private void initRecycleView() {
        ArrayList itemData = new ArrayList();
        itemData.add("res://com.superservice.lya/" + R.mipmap.ic_tianjia);


        releaseWDPIcAdapter = new ReleaseWDPIcAdapter(this, itemData);
        releaseWDPIcAdapter.setCallBack(new ReleaseWDPIcAdapter.CallBack() {
            @Override
            public void OnClick() {


                if (popupWindowCutPic == null) {
                    popupWindowCutPic = new PopupWindowCutPic(workType);
                    popupWindowCutPic.setOnClickListener(new PopupWindowCutPic.CutPicCallBack() {
                        @Override
                        public void choose() {

                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                            takePhoto.onPickMultiple(6);
//                            takePhoto.onPickFromGalleryWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                        }

                        @Override
                        public void cancel() {

                        }

                        @Override
                        public void camera() {
                            takePhoto.onEnableCompress(Utils.takePhotoCompress(), true);
                            takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                        }

                        @Override
                        public void see() {

                        }
                    });

                }
                isCompanyLogo = false;
                popupWindowCutPic.isShow();
//                Uri imageUri = Uri.fromFile(Utils.getLiZiPicFile());
//                takePhoto.onPickFromGalleryWithCrop(imageUri, getCropOptions());
            }
        });

        GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 3);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(releaseWDPIcAdapter);
    }

}
